<script lang="ts" setup>
import { ref } from 'vue';
import { useVbenDrawer } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import { detailApi, editApi } from '#/api/system/setting';

const key = ref('');

const [Form, formApi] = useVbenForm({
  schema: [
    {
      fieldName: 'points_name',
      label: '积分名称',
      component: 'Input',
    },
    {
      fieldName: 'describe',
      label: '积分说明',
      component: 'Textarea',
      componentProps: {
        rows: 5,
      },
      formItemClass: 'items-start',
      labelClass: 'pt-1',
    },
  ],
  showDefaultActions: false,
});

const [Drawer, drawerApi] = useVbenDrawer({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) return;
    const values = await formApi.getValues();
    drawerApi.lock();
    editApi(key.value, values)
      .then((res) => {
        message.success({
          content: res.message,
        });
        drawerApi.close();
      })
      .catch(() => {
        drawerApi.unlock();
      });
  },
  onOpenChange(isOpen) {
    if (isOpen) {
      const data = drawerApi.getData();
      formApi.resetForm();
      if (data.key) {
        key.value = data.key;
        drawerApi.setState({ title: data.describe })
        detailApi(data.key).then((res) => {
          formApi.setValues(res.values);
        })
      }
    }
  },
  overlayBlur: 1,
  title: '',
//   class: 'w-[380px]',
});
</script>
<template>
  <Drawer>
    <Form />
  </Drawer>
</template>
